//
//  main.m
//  OC04_NSString_NSNumber_NSValue
//
//  Created by Rain on 16/1/28.
//  Copyright © 2016年 Rain. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef struct {
    char name[20];
    int age;
}Student;

/** 知识点: NSString. */ //不可变字符串
void chapterOfNSString() {
    
    /** NSString: 创建以后, 内容和长度不能改变. */
#if 0
    /** 初始化(init)  */
    NSString *str1 = [[NSString alloc] initWithFormat:@"Lanou"];
    /** 便利构造器(string) */
    NSString *str2 = [NSString stringWithFormat:@"%@ Love",str1];
    /** 获取字符串长度(length) */
    NSLog(@"str2 length: %ld",str2.length);
    /** 获取字符串中字符(character) */
    unichar c = [str2 characterAtIndex:3];
    NSLog(@" %c",c);
    /** 字符串是否相等(equal) */
    NSString *str3 = [NSString stringWithFormat:@"dalian"];
    NSString *str4 = [NSString stringWithFormat:@"%@" ,str3];
    BOOL a = [str1 isEqualToString:str2];
    //isEqualToString判断两个字符串的内容是否相等
    a = [str3 isEqualToString:str4];
    if(a){
        NSLog(@"str3 和 str4 相等");
    } else {
        NSLog(@"str3 和 str4 不相等");
    }
    // == 是判断两个字符串对象是否指向同一个内存.
    NSString *str5 = @"dalian";
    if(str3 == str5) {
        NSLog(@"相等");
    } else {
        NSLog(@"不相等");
    }
#endif
    /** 获取子串(sub) */
#if 0
    
    // substringFromIndex ,从给定的下标开始一直到字符串最后
    NSString *str6 = [NSString stringWithFormat:@"dalian changchun shenyang"];
    NSString *subStringOfStr6 = [str6 substringFromIndex:7];
    NSLog(@"subStringOfStr6: %@",subStringOfStr6);
    // substringToIndex ,从给定的下表开始往前的字符串.
    NSString *str8 = [NSString stringWithFormat:@"dalian changchun shenyang"];
    NSString *str9 = [str8 substringToIndex:7];
    NSLog(@"%@",str9);
    
    //NSMakeRange(loc, len): 创建结构体变量
    // loc代表起始位置下标
    // len代表长度
    NSRange range = NSMakeRange(2, 6);
    NSString *str10 = [str6 substringWithRange:range];
    NSLog(@"%@",str10);
    
    
    /** 拼接字符串(append)  */
    NSString *str11 = [NSString stringWithFormat:@"zhaowuyi"];
    NSString *appendingStringOfStr6 = [str11 stringByAppendingFormat:@"%@ dalian",str6];
    NSLog(@"%@",appendingStringOfStr6);
    //当NSString(不可变字符串)类的对象的时候,都是返回一个新的字符串.
    
    
    NSString *str12 = [str11 stringByAppendingString:str6];
    NSLog(@"%@", str12);
    /** 替换字符串(replace) */
    NSString *str13 = [NSString stringWithFormat:@"sichangbao"];
    NSString *replaceOfStr13 = [str13 stringByReplacingCharactersInRange:NSMakeRange (2, 4) withString:@"hei"];
    NSLog(@"%@",replaceOfStr13);
    /** 字符串对象转化为int类型(int) */
    NSString *strNumber = [NSString stringWithFormat:@"123456"];
    NSInteger x = strNumber.intValue;
    NSLog(@"%ld",x);
    /** 字符串对象转化为其他数据类型, 例如double, float, BOOL等. */
    NSString *strDouble = [NSString stringWithFormat:@"12.345"];
    double y = strDouble.doubleValue;
    NSLog(@"%lf",y);
    
    double d = 32.456;
    NSString *str14 = [NSString stringWithFormat:@"%.3lf",d];
    NSLog(@"str14 = %@",str14);
    
    
    NSString *strFloat = [NSString stringWithFormat:@"23.456"];
    float z = strFloat.floatValue;
    NSLog(@"%f",z);
    
    
    /** 字符串全部大写(uppercase) */
    
    NSString *strDa = [NSString stringWithFormat:@"woShiSHEI"];
//    strDa.uppercaseString;
    NSLog(@"%@", strDa.uppercaseString);
    
    /** 字符串全部小写(lowercase) */
    
    NSString *strXiao = [NSString stringWithFormat:@"woShiSHEI"];
    NSLog(@"%@", strXiao.lowercaseString);
    /** 字符串首字母大写(capitalized) */
    NSString *strFirstDa = [NSString stringWithFormat:@"ok ok ok ok ok"];
    NSLog(@"%@",strFirstDa.capitalizedString);
    
    /** 是否以指定字符串为前缀(prefix) */
    NSString *strPrefix = [NSString stringWithFormat:@"sichangbao"];
    BOOL strResult = [strPrefix hasPrefix:@"sicha"];
    
    NSLog(@"%d",strResult);
    /** 是否以指定字符串为后缀(suffix) */
    NSString *suffix = [NSString stringWithFormat:@"sichangbao"];
    
    strResult = [suffix hasSuffix:@"bao"];
    NSLog(@"%d",strResult);
#endif
    /** 字面量 */
  //  NSString *str =@"zimianliang";
    
    /** 练习: http://www.lanou3g.com/icon.png 判断上述字符串中是否以“png”结尾，如果是就替换成“jpg”，如果不是，就拼接”.jpg”。*/
    NSString *address = [NSString stringWithFormat:@"http://www.lanou3g.com/icon.png"];
    NSString *newStr =[NSString string];
    if ([address hasSuffix:@"png"]) {
        newStr = [address stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"];
    } else {
        newStr =[address stringByAppendingString:@".jpg"];
    }
    NSLog(@"%@",newStr);
    
    
}

/** 知识点: NSMutableString. */
void chapterOfNSMutableString() {
    /**
     * NSMutableString: 创建以后内容还可以修改, 可以进行增, 删, 改等操作.
     * 是NSString的子类
     * 注: 涉及到可变的类, 关键字是mutable.
     */
    
    
    /** 初始化方法(init) */
    NSMutableString *mStr1 = [[NSMutableString alloc] initWithCapacity:10];
        // 在创建可变字符串的时候,可以按照期望的字符数量进行内存分配.
    NSMutableString *mStr2 = [[NSMutableString alloc] initWithFormat:@"wanghuaqian"];
        //可以调用父类的初始化方法
    
    /** 便利构造器(string) */
    NSMutableString *mStr3 = [NSMutableString stringWithCapacity:10];
    
    [mStr3 setString:@"ssss"];
    NSLog(@"%@",mStr3);
    
    /** 拼接字符串(append) */
    
    [mStr2 appendFormat:@"%@ 被玩坏了",mStr3];
    NSLog(@"%@",mStr2);
    
    //子类特有的拼接方法
    [mStr2 appendString:@"555"];
    NSLog(@"拼接之后: %@",mStr2);
    
    
    
    
    // 调用父类的拼接方法.
    [mStr2 stringByAppendingString:@"is laowang"];
    /** 插入字符串(insert) */
    NSMutableString *stu4 = [NSMutableString stringWithCapacity:5];
    [stu4 setString:@"123456789"];
    [stu4 insertString:@"abc" atIndex:4];
    NSLog(@"%@",stu4);
    //注意: 下标不要越界.
    
    
    /** 删除字符串(delete) */
    [stu4 deleteCharactersInRange:NSMakeRange(2, 5)];
    NSLog(@"%@",stu4);
    /** 替换字符串(replace) */
    [stu4 replaceCharactersInRange:NSMakeRange(2, 5) withString:@"edcba"];
    NSLog(@"%@",stu4);
    
    
    /** 重置字符串(set) */
    [stu4 setString:@"987654"];
    NSLog(@"%@",stu4);
    /**
     * 不可变字符串:
     * 本身不能被修改.
     * 对不可变字符串的修改操作, 都是操作字符串的副本, 得到一个新的字符串并返回.
     * 可变字符串:
     * 本身能被修改.
     * 可变字符串修改的是原字符串, 因此可变字符串的操作方法没有返回值.
     */
    
    
    /** 练习: 用NSMutableString完成下述功能：
     http://www.lanou3g.com/icon.png
     判断上述字符串中是否以“png”结尾，如果是就替换成“jpg”，如果不是，就拼接”.jpg”。
     */
    NSMutableString *mstrk = [NSMutableString stringWithFormat:@"http://www.lanou3g.com/icon.png"];
    if([mstrk hasSuffix:@"png"]){
        [mstrk replaceCharactersInRange:NSMakeRange(mstrk.length - 3,3) withString:@"jpg"];
    } else {
        [mstrk appendFormat:@".jpg"];
    }
    NSLog(@"%@",mstrk);
    
    /**
     字面量
     */
    NSMutableString *mStrkk = @"liuaobo".mutableCopy;

}

/** 知识点: NSNumber. */
void chapterOfNumber() {
    
    /** 作用: 实现基本数据类型和对象类型的相互转化. */
    
    int a = 23;
    NSNumber *intNumber = [NSNumber numberWithInt:a];
    NSLog(@"intNumber: %@",intNumber);
    /** int类型转化为NSNumber类型的对象 */
    
    
    
    float b = 12.3;
    NSNumber *floatNumber = [NSNumber numberWithFloat:b];
    NSLog(@"floatNumber: %@",floatNumber);
    /** float类型转化为NSNumber类型的对象 */
    
    
    
    char c = 'w';
    NSNumber *charNumber = [NSNumber numberWithChar:c];
    NSLog(@"charNumber: %@",charNumber);
    /** char类型转化为NSNumber类型的对象 */
    
    
    
    int x = intNumber.intValue;
    NSLog(@"%d",x);
    
    /** NSNumber类型对象转化为int类型的数据 */
    
    
    
    float y = floatNumber.floatValue;
    NSLog(@"%.2f",y);
    /** NSNumber类型对象转化为float类型的数据 */
    
    char z = charNumber.charValue;
    NSLog(@"%c",z);
    /** NSNumber类型对象转化为char类型的数据 */
    
    
    NSNumber *nsn1 = [NSNumber numberWithInt:50];
    NSNumber *nsn2 = [NSNumber numberWithInt:52];
    NSLog(@"%ld",[nsn1 compare:nsn2]);
    
    switch ([nsn1 compare:nsn2]) {
        case NSOrderedAscending:
            NSLog(@"nsn1 比 nsn2 小");
            break;
        case NSOrderedDescending:
            NSLog(@"nsn1 比 nsn2 大");
            break;
        case NSOrderedSame:
            NSLog(@"nsn1 和 nsn2 一样大");
            break;
            
        default:
            break;
    }
    /** NSNumber类型对象的比较(compare) */
   
    
    
    NSNumber *number1 = @12;
    NSNumber *number2 = @12.3456;
    NSNumber *number3 = @'r';
    NSNumber *number4 = @(45);
    
    //当使用字面量时,把变量转换成NSNumber,一定要使用有()的形式.
    int w = 23;
    NSNumber *number5 = @(w);
    /** 字面量 */
    
}

/** 知识点: NSValue. */
void chapterOfNSValue() {
#if 0
    /** 作用之一: 结构体和对象类型的转化. */
    
    /** NSRange类型转化为NSValue类型的对象 */
    NSRange range = NSMakeRange(2, 10);
    NSValue *rangeValue = [NSValue valueWithRange:range];
    NSLog(@"rangeValue: %@",rangeValue);
    
    /** NSValue类型转化为NSRange类型的结构体变量 */
    NSRange otherRange = rangeValue.rangeValue;
    NSLog(@"%ld ,%ld",otherRange.location,otherRange.length);
    
    /** 指针转化为NSValue.(pointer) */
    int a = 23;
    int *p = &a;
    NSValue *value = [NSValue valueWithPointer:p];
    NSLog(@"value: %@", value);
    
    int *p1 = value.pointerValue;
    
    
#endif
    
    /** NSValue 转化为 指针. */
    
    
    /** 自定义结构体转化为NSValue对象 */
    Student stu = {"Tom", 23};
    NSValue *valueWithStudent = [NSValue valueWithBytes:&stu objCType:@encode(Student)];
    NSLog(@"valueWithStudent: %@",valueWithStudent);
    /** NSValue对象转化为自定义结构体 */
    Student *stu1 = malloc(sizeof(Student));
    [valueWithStudent getValue:stu1];
    NSLog(@"name: %s, age: %d",stu1->name, stu1->age);
    
    Student stu2 = {"",0};
    [valueWithStudent getValue:&stu2];
    NSLog(@"name: %s, age :%d",stu2.name,stu2.age);
}




#pragma mark - MAIN FUNCTION
int main(int argc, const char * argv[]) {
    
#pragma mark - 知识点1: NSString(不可变字符串)
//    chapterOfNSString();
    
    
#pragma mark - 知识点2: NSMutableString(可变字符串)
//    chapterOfNSMutableString();
    
    
#pragma mark - 知识点3: NSNumber(数值类)
    chapterOfNumber();
    
    
#pragma mark - 知识点4: NSValue
    chapterOfNSValue();
    
    

    
    return 0;
}
